using System;

using octalforty.Componento.Core.Util;

namespace octalforty.Componento.Core.ObjectModel
{
    public class VersionInterval
    {
        /// <summary>
        /// Gets minimum version requirement or <c>null</c>.
        /// </summary>
        public Version Min
        { get; private set; }

        /// <summary>
        /// Gets maximum version requirement or <c>null</c>.
        /// </summary>
        public Version Max
        { get; private set; }

        public VersionIntervalType Type
        { get; private set; }

        public VersionInterval(Version min, Version max, VersionIntervalType type)
        {
            Min = min;
            Max = max;
            Type = type;
        }

        public VersionInterval(string min, string max, VersionIntervalType type) :
            this(
                min.IsNullOrEmptyString() ? null : new Version(min), 
                max.IsNullOrEmptyString() ? null : new Version(max), 
                type)
        {
        }

        public bool Equals(VersionInterval other)
        {
            if(ReferenceEquals(null, other)) return false;
            if(ReferenceEquals(this, other)) return true;
            return Equals(other.Min, Min) && Equals(other.Max, Max) && Equals(other.Type, Type);
        }

        public override bool Equals(object obj)
        {
            if(ReferenceEquals(null, obj)) return false;
            if(ReferenceEquals(this, obj)) return true;
            if(obj.GetType() != typeof(VersionInterval)) return false;
            return Equals((VersionInterval)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = (Min != null ? Min.GetHashCode() : 0);
                result = (result * 397) ^ (Max != null ? Max.GetHashCode() : 0);
                result = (result * 397) ^ Type.GetHashCode();
                return result;
            }
        }
    }
}